package com.wang.tool.common;

import java.text.MessageFormat;

/**
 * 通用异常
 *
 * @Author wangshaopeng
 * @Date 2020/12/7
 */
public class SimpleException extends RuntimeException {
    private static final long serialVersionUID = -4126221976400623164L;
    public static final String SYSTEM_ERROR = "SYSTEM_ERROR";
    public static final String UNKNOWN_ERROR = "UNKNOWN_ERROR";
    public static final String PARAM_INVALID = "PARAM_INVALID";
    public static final String CONFIG_INVALID = "CONFIG_INVALID";
    public static final String RPC_TIMEOUT = "RPC_TIMEOUT";
    public static final String LOCK_FAIL = "LOCK_FAIL";
    public static final String REJECT_ERROR = "REJECT_ERROR";
    public static final String OPERATION_CONFLICT = "OPERATION_CONFLICT";
    public static final String RPC_ERROR = "RPC_ERROR";
    public static final String NPE = "NPE";
    public static final String TIME_OUT = "TIME_OUT";
    public static final String UN_SUPPORT = "UN_SUPPORT";
    public static final String UN_EXPECT = "UN_EXPECT";
    public static final String NOT_FOUND = "NOT_FOUND";
    public static final String DATA_EMPTY = "DATA_EMPTY";
    public static final String DATA_INVALID = "DATA_INVALID";
    public static final String STATUS_INVALID = "STATUS_INVALID";
    public static final String BIZ_FLOW_LIMIT = "BIZ_FLOW_LIMIT";
    public static final String BIZ_DOWNGRADE = "BIZ_DOWNGRADE";
    public static final String EXECUTOR_BUSY = "EXECUTOR_BUSY";
    public static final String THREAD_INTERRUPT = "THREAD_INTERRUPT";
    public static final String HTTP_ERROR = "HTTP_ERROR";
    public static final String ENCODE_ERROR = "ENCODE_ERROR";
    public static final String DECODE_ERROR = "DECODE_ERROR";
    public static final String GET_ERROR = "GET_ERROR";
    public static final String SET_ERROR = "SET_ERROR";
    public static final String DAO_ERROR = "DAO_ERROR";



    private String code;
    private String message;

    public SimpleException(String message) {
        super(message);
        this.code = UNKNOWN_ERROR;
        this.message = getMessage();
    }


    /**
     * @param code    异常码
     * @param pattern 信息 参数异常{0}不可为空
     * @param args    eg:商品id
     */
    public SimpleException(String code, String pattern, Object... args) {
        super(pattern == null ? null : MessageFormat.format(pattern, args));
        this.message = getMessage();
        this.code = code;
    }


    /**
     * @param cause   源异常
     * @param message 异常码
     */
    public SimpleException(Throwable cause, String message) {
        super(message, cause);
        this.message = getMessage();
    }

    /**
     * @param cause   源异常
     * @param code    异常码
     * @param message 异常信息
     */
    public SimpleException(Throwable cause, String code, String message) {
        super(message, cause);
        this.code = code;
        this.message = getMessage();
    }

    @Override
    public String getMessage() {
        return message == null ? super.getMessage() : message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "{code:\"" + code +
                "\",message:\"" + getMessage() +
                "\"}";
    }
}
